package ui;

import com.sun.security.ntlm.Server;
import server.Out;
import server.ServerMain;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.InetAddress;

public class Frame extends JFrame implements WindowListener {
    static final Color green=new Color(70,185,55),dgreen=new Color(180,220,180),red=new Color(230,70,70);
    static final Font font=new Font("黑体",Font.BOLD,65),inf=new Font("黑体",Font.PLAIN,35);
    static final BasicStroke bs=new BasicStroke(6);
    class startBtn extends JButton{
        public void paint(Graphics g){
            g.setColor(ServerMain.state==ServerMain.FREE?green:dgreen);
            g.fillOval(0,0,this.getWidth(),this.getHeight());

            g.setColor(Color.white);
            //g.drawString("开始",this.getWidth()/2,this.getHeight()/2);
            ((Graphics2D)g).setStroke(bs);
            int sx=18,sy=15,w=28,h=28;
            g.drawLine(sx,sy,sx,sy+h);
            g.drawLine(sx,sy,sx+w,sy+h/2);
            g.drawLine(sx,sy+h,sx+w,sy+h/2+1);

        }
    }
    class infoBtn extends JButton{
        public void paint(Graphics g){
            g.setColor(ServerMain.state==ServerMain.COUNT?red:Color.orange);
            g.fillOval(0,0,this.getWidth(),this.getHeight());

            g.setColor(Color.white);
            g.setFont(font);
            if(ServerMain.state==ServerMain.COUNT||ServerMain.state==ServerMain.RUSH)
                g.drawString(""+panel.countDownTime,this.getWidth()/2-17,this.getHeight()/2+20);

        }
    }
    class pgmInfo extends JButton{
        public void paint(Graphics g){
            g.setColor(Color.gray);
            ((Graphics2D )g).setStroke(new BasicStroke(5));
            g.drawOval(2,2,this.getWidth()-4,this.getHeight()-4);
            g.setFont(inf);
            g.drawString("i",this.getWidth()/2-8,this.getHeight()/2+12);
        }
    }

    public Panel panel=new Panel();
    JButton start=new JButton("Start");
    public startBtn sb=new startBtn();
    infoBtn ib=new infoBtn();
    pgmInfo pi=new pgmInfo();
    public Frame(){
        this.setLayout(null);
        this.setLocation(0,0);
        int screenWidth=((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width);
        int screenHeight = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);

        this.setSize(screenWidth,screenHeight-40);
//        this.setUndecorated(false);
//        this.getGraphicsConfiguration().getDevice()
//                .setFullScreenWindow(this);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);


        Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
        //this.setSize(d);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(this);
        this.setTitle("Rushhhhhhhhhh");

        sb.setSize(60,60);
        sb.setLocation((int)(this.getWidth()*0.2),(int)(this.getHeight()*0.5));
        this.add(sb);
        ib.setSize(80,80);
        ib.setLocation((int)(this.getWidth()-this.getWidth()*0.2-25),(int)(this.getHeight()*0.5));
        this.add(ib);
        pi.setSize(40,40);
        pi.setLocation(3,3);
        pi.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                javax.swing.JOptionPane.showMessageDialog(ServerMain.mf,"Bug反馈邮箱：1010553892@qq.com\n\n" +
                        "使用方法：1启动电脑端，查看标题栏的ip\n" +
                        "2使安卓手机与电脑处于同一wifi，启动安卓端，在设置内输入id（仅能输入数字），ip，端口写5400\n" +
                        "3点击连接，连接成功会在电脑端下方显示列表\n\n" +
                        "注意：1不可有重复id的手机端\n2若无法连接，请尝试切换一个网络");
            }
        });
        this.add(pi);

        start.setSize(90,30);
        start.setLocation(0,0);
        sb.addActionListener((ActionEvent event)->{
            sb.setEnabled(false);
            new CountDown().start();
        });
        //this.add(start);


        panel.setLocation(0,0);
        panel.setSize(this.getSize());
        this.add(panel);
        this.setSize(this.getWidth(),this.getHeight()+40);

        this.setVisible(true);

        try {
            InetAddress ia = InetAddress.getLocalHost();
            this.setTitle("Rush-ip:" + ia.getHostAddress());
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void rushSuccess(int clientId){
        Out.say("clinet "+clientId+" rush successed");
        ServerMain.mf.panel.idToDisplay=clientId;
        ServerMain.state=ServerMain.FREE;
        ServerMain.mf.panel.repaint();
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        this.setVisible(false);
        this.dispose();
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
